<?php

class Dealscombined_Crawlers_Wahanda_SiteProcessor extends Dealscombined_SiteProcessor_ProcessorAbstract
{
		
				
	protected function normaliseUrl($url, $baseUrl) 
	{
		$url = parent::normaliseUrl($url, $baseUrl);
		return $url.'#rpp=1000';
	}
	
	protected function getDepartmentsAndSubdepartments($url, $userAgent)
	{	
		$html  = $this->getRawData($url, $userAgent);
		$xpath = $this->getXpath($html);
		
		$departments    = $xpath -> query("//div[contains(@class,'nav')]/table//div/a");
		$subdepartments = $xpath -> query("//div[contains(@class,'nav')]//td//ul/li/a");
		
		foreach ($departments as $department) {
		
			$finalDepartment = array(
				'name' => $department->getAttribute('title'),
				'url'  => $this->normaliseUrl($department->getAttribute('href'), $url),
				'type' => 'department',
				'checked' => 1
			);
		
			try{
				$this->_database->insert('sections', $finalDepartment);
			}
			catch(Exception $e){
				echo 'Error with Db: '.$e->getMessage()."\n <br/>"; 
			}
		}
		
		foreach ($subdepartments as $subdepartment) {
		
			$finalSubdepartment = array(
				'name'    => utf8_decode($subdepartment->nodeValue),
				'url'     => $this->normaliseUrl($subdepartment->getAttribute('href'), $url),
				'type'    => 'subdepartment',
				'checked' => 1
			);
			
			try{
				$this->_database->insert('sections', $finalSubdepartment);
			}
			catch(Exception $e){
				echo 'Error with Db: '.$e->getMessage().PHP_EOL;
			}
		}
	}

	protected function getProductListing()
	{	
		$select = $this->_database->select()->from('sections', array('id', 'url'))
											->where("type != 'department'");	
		$stmt = $this->_database->query($select);
		$results = $stmt->fetchAll();
		
		foreach ($results as $result) {
			echo 'Processing: '.$result['url'].'<br/>';
			$html  = $this->getRawData($result['url']);
			$xpath = $this->getXpath($html);
			
			$location  = $xpath->query("//html//div[contains(@class,'result')]//p[contains(@class,'location')]"); 
			$products  = $xpath->query("//html//div[contains(@class,'result')]//table[@class='menu']");
		
			foreach($products as $product){
				
				$data = array();
				//$data['site_id'] = $result['id'];
			
				
			    $innerHTML= '';
			    $children = $product->childNodes;
			    foreach ($children as $child) {
			        $innerHTML .= $child->ownerDocument->saveXML($child);
			    } 
				
				$productXpath = $this->getXpath($innerHTML);							
				$name = $productXpath->query("//tr[@class='menu-item action-container']/td[contains(@class,'name')]/div");
				
				foreach ($name as $value) {
					$data['title'] = $value->getAttribute('data-name');
					$data['price']  = $this->getPrice($value->getAttribute('data-sale-price-amount'));
					$data['price_was'] = $this->getPrice($value->getAttribute('data-rrp-price-amount'));
					$data['customer_rating']  = $value->getAttribute('data-average-rating');
				}
				
				$discount = $productXpath->query("//span[@class='discount']");
				foreach ($discount as $value) {
					$data['discount']  = $value->nodeValue;
				} 
				
				$url  = $productXpath->query("//tr[@class='menu-item action-container']/td[contains(@class,'name')]/a");
				foreach ($url as $value) {
					$data['url']  = $value->getAttribute('href');
				}
				$data['url'] = !isset($data['url']) || is_null($data['url']) ? null : parent::normaliseUrl($data['url'],$this->_url);
				
				$location = $productXpath->query("//p[@class='location']");
				foreach ($location as $value) {
					$data['location']  = $value->nodeValue;
				}

				$thumbnailUrl = $productXpath->query('//div/img');
				foreach ($thumbnailUrl as $value) {
					$data['thumbnail_url']  = $value->getAttribute('src');
				}
				 
				$data['extraction_time'] = date("Y-m-d H:i:s");
				
				try{
					if (isset($data['title']) && !is_null($data['title']) && isset($data['url']) && !is_null($data['url']) ) 
			    		$this->_database->insert('deals', $data);
				}
				catch(Exception $e){
					echo 'Error with Db: '.$e->getMessage().PHP_EOL;
				}	
			}
		}
	}

	private function getProducDetails()
	{
		$select = $this->_database->select()->from('deals', array('id', 'url'));
		$stmt = $this->_database->query($select);
		$results = $stmt->fetchAll();
		
		foreach ($results as $result) {
			echo 'Processing: '.$result['url'].PHP_EOL;
			$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
			sleep(1);	
			$html  = $this->getRawData($result['url'], $userAgent);
			
			var_dump($html);
			
			$xpath = $this->getXpath($html);
			
			
				$description         = $xpath->query("//html//div[contains(@class,'offer-info')]/div[not(contains(@id,'social'))]//*");
				foreach ($description as $value) {
					$data['description']  = $value->nodeValue;
				} 
				$short_title         = $xpath->query("//html//div[@id='merchandising-module']//h1[contains(@class,'short-name')]");
				foreach ($short_title as $value) {
					$data['short_title']  = $value->nodeValue;
				} 
				$customer_rating     = $xpath->query("//html//div[@id='merchandising-module']//p[@class='rating']/span[@class='value-title']");
				foreach ($customer_rating as $value) {
					$data['customer_rating']  = $value->nodeValue;
				} 
				$location            = $xpath->query("//html//div[@id='merchandising-module']//p[@class='rating']/span[2]/a");
				foreach ($location as $value) {
					$data['location']  = $value->nodeValue;
				} 
				$percentage_discount = $xpath->query("//html//div[@id='merchandising-module']//div[@class='product-info']//div[@class='discount']");
				foreach ($percentage_discount as $value) {
					$data['percentage_discount']  = $value->nodeValue;
				} 
				$price               = $xpath->query("//html//div[@id='merchandising-module']//div[@class='product-info']//div[@class='price']//text() | //html//div[@id='merchandising-module']//div[@class='product-info']//div[@class='price ']//text()");
				foreach ($price as $value) {
					$data['price']  = $value->nodeValue;
				} 
				$price_was           = $xpath->query("//html//div[@id='merchandising-module']//div[@class='product-info']//div[@id='discount-details']//span[@class='info value']");
				foreach ($price_was as $value) {
					$data['price_was']  = $value->nodeValue;
				} 
				$discount            = $xpath->query("//html//div[@id='merchandising-module']//div[@class='product-info']//div//span[@class='offer-detail']//span[@class='info saving']");
				foreach ($discount as $value) {
					$data['discount']  = $value->nodeValue;
				} 
				$image_url           = $xpath->query("//html//div[@id='merchandising-module']//img//@src");
				foreach ($image_url as $value) {
					$data['image_url']  = $value->nodeValue;
				} 
				$title               = $xpath->query("//html//div[@id='merchandising-module']//img//@alt");
				foreach ($title as $value) {
					$data['title']  = $value->nodeValue;
				} 
				$top_deal            = $xpath->query("//html//a[contains(@class,'badge')]//img/@alt");
				foreach ($top_deal as $value) {
					$data['top_deal']  = $value->nodeValue;
				}  
			
			
			/*$data = array();
			foreach ($attributes as $key => $value) {
				$data[$key] = null;
				foreach ($value as $val) {
					$data[$key] = $val->nodeValue;
				}
				if (is_null($data[$key]) ){
					unset($data[$key]);
				}
			}*/
			
			$data['extraction_time'] = date("Y-m-d H:i:s");
			try{
					$tempQuery = 'UPDATE deals SET desc = IF '	;
						
					$this->_database->update('deals', $data, $result['id']);
			}catch(Exception $e){
				echo 'Error with Db: '.$e->getMessage().PHP_EOL;
			}
		}
	}

	
	public function process($userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)')
	{
		/*echo 'Starting Time Departments and Sumbdepartments: '.date("Y-m-d H:i:s").PHP_EOL;
		$this->getDepartmentsAndSubdepartments($this->_url, $userAgent);
		echo 'Finishing Time: '.date("Y-m-d H:i:s").PHP_EOL."\n <br/>";	
		
		sleep(3);
		
		echo 'Starting Time Product Listing: '.date("Y-m-d H:i:s").PHP_EOL."\n <br/>";
		$this->getProductListing();
		echo 'Finishing Time: '.date("Y-m-d H:i:s").PHP_EOL."\n <br/>";*/
		 
		echo 'Starting Time Product Details: '.date("Y-m-d H:i:s").PHP_EOL;
		$this->getProducDetails();
		echo 'Finishing Time: '.date("Y-m-d H:i:s").PHP_EOL;
		
		
	
	}	
	
	private function getRating($rating){
		
	$matches = array();
	preg_match("/ratings-([0-9]+)/", $rating, $matches);

	return isset($matches[1]) ? $matches[1] / 2 : 0;

	}
	
}		
		